<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/14 10:50
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\apiadmin\controller;


use app\common\model\admin\MenuModel;
use app\common\model\admin\UserRoleModel;
use app\common\service\TreeService;
use app\common\utils\Res;
use app\common\service\RouterService;

class Menu extends AuthController
{

    /**
     * @return \think\response\Json
     * 获取用户的权限菜单
     */
    public function getRouter(){
        $roleresult = UserRoleModel::findByUserRoleId($this->request->user_id);
        $roleIds=[];
        foreach ($roleresult as $k=>$v){
            $roleIds[]=$v['role_id'];
        }
        if(in_array(1,$roleIds)){
            $result = MenuModel::adminMenuByUserName();
        }else{
            $result = MenuModel::userMenuByUserName($this->request->username);
        }
        $routerList =  RouterService::MakeRouter($result,'menu_id','parent_id','menu_name');
        return Res::ok("查询成功",$routerList);
    }


    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 查询所有的菜单
     */
    public function findMenuTree(){
       $menurulst = MenuModel::findByAll();
       $data = TreeService::MakeMenuTree($menurulst,'menu_id','parent_id');
       $rulesttype = MenuModel::findByType();
       $dataTree = TreeService::MakeTreePid($rulesttype,'menu_id','parent_id');
       $datas=[
           'menuTree'=>$data,
           'pTree'=>$dataTree
       ];
       return Res::ok("查询成功",$datas);
    }


    /**
     * @return \think\response\Json
     * 添加菜单
     */
    public function add(){
        $param = $this->request->post();
        $param['create_by']=$this->request->username;
        $param['create_time']=date('Y-m-d H:i:s',time());
        $result = MenuModel::add($param);
        return Res::jsonResult($result);
    }

    public function edit(){
        $param = $this->request->post();
        $param['update_by']=$this->request->username;
        $param['update_time']=date('Y-m-d H:i:s',time());
        $result = MenuModel::edit($param);
        return Res::jsonResult($result);
    }

    /**
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del($id){
        $result = MenuModel::del($id);
        return Res::jsonResult($result);
    }

    /**
     * @param $id
     * @return \think\response\Json
     *
     */
    public function changeStatus($id){
        $result = MenuModel::changeStatus($id);
        return Res::jsonResult($result);
    }

    /**
     * @return \think\response\Json
     */

    public function changeOrder()
    {
        $param = $this->request->post();
        $result = MenuModel::changeOrder($param);
        return Res::jsonResult($result);
    }

}